package parse

import (
	"strings"
)

const (
	arrayValueSeparator    = ","
	keyValueSeparator      = ":"
	keyOpSeparator         = ","
	arrayKeyValueSeparator = ";"
)

func ParsePageInfo(
	current *uint64,
	size *int64,
	defaultSize int64,
	maxSize int64,
) (uint64, uint64) { // #24.02.29#
	var page uint64 = 1
	var pageSize int64
	if current != nil && *current > 1 {
		page = *current
	}

	if size != nil {
		// 0或-1表示查询全部
		if *size > 0 {
			pageSize = *size
			if pageSize > maxSize {
				pageSize = maxSize
			}
		}
	} else {
		pageSize = defaultSize
	}

	return page, uint64(pageSize)
}

func ParseFilter(filter *string) (map[string]string) {
	if filter != nil && len(*filter) > 0 {
		filters := strings.Split(*filter, arrayKeyValueSeparator)
		if len(filters) > 0 {
			res := make(map[string]string)
			for _, group := range filters {
				parts := strings.Split(group, keyValueSeparator)
				if len(parts[0]) > 0 && len(parts[1]) > 0 {
					res[parts[0]] = parts[1]
				} else if len(parts[0]) > 0 {
					res[parts[0]] = "1"
				}
			}

			return res
		}
	}

	return nil
}
